﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Events.*;
    import Display.DialogUI.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Display.MarketUI.*;
    import Display.NeighborUI.*;
    import Engine.*;

    public class IdleDialogManager {

        public static const IDLE_ADDNEIGHBOR:int = 0;
        public static const IDLE_HELPFRIEND:int = -1;
        public static const IDLE_SENDGIFT:int = 1;
        public static const IDLE_LOSTPUPPY:int = 2;
        public static const IDLE_FLASHSALE:int = 3;
        public static const IDLE_COUNT:int = 4;
        private static const m_IdleData_TYPE:int = 0;
        private static const m_IdleData_PROBABILITY:int = 1;
        private static const m_IdleData_VALIDFUNCTION:int = 2;
        private static const m_IdleData_IDLEDIALOGTEXT:int = 3;
        private static const m_IdleData_IDLEDIALOGTITLE:int = 4;
        private static const m_IdleData_IDLEDIALOGBUTTON:int = 5;
        private static const IDLE_DEBUG:Boolean = false;
        private static const DEBUG_SCREEN:int = 1;

        private static var m_NotDoneInit:Boolean = true;
        private static var m_IdleDialogPopupDelayTime:Number = -1;
        private static var m_IdleTimerStart:Number = -1;
        private static var m_LastMouseX:Number = -1;
        private static var m_LastMouseY:Number = -1;
        private static var m_showingIdleDialog:Boolean = false;
        private static var m_PossibleChoices:Array = new Array();
        private static var m_PickedDialog:int = -1;
        private static var m_SkippedFlags:Array = new Array();
        private static var m_IdleDialogData:Array = new Array(new Array(IDLE_ADDNEIGHBOR, 3, test_AddNeighbor, "IdleAddNeighbors_text", "IdleAddNeighbors", "IdleAddNeighbors_button"), new Array(IDLE_SENDGIFT, 1, test_SendAGift, "IdleGiftFriends_text", "IdleGiftFriends", "IdleGiftFriends_button"), new Array(IDLE_LOSTPUPPY, 0, test_LostPuppy, "lost_puppy_body", "lost_puppy", "lost_puppy_ok"), new Array(IDLE_FLASHSALE, 5, test_flashSale, "", "", ""));
        private static var m_DefaultDialog:int = 1;
        private static var strFriendName:String = "";
        private static var strFriendHelpID:String = "";
        private static var m_catalogWindow:Catalog;
        private static var m_notDoneIdle:Boolean = true;
        private static var toggleHotKey:Boolean = false;

        private static function doInitIfNecessary():void{
            var _local1:int;
            if (m_NotDoneInit){
                _local1 = 0;
                while (_local1 < IDLE_COUNT) {
                    m_SkippedFlags.push(false);
                    _local1++;
                };
                m_notDoneIdle = true;
                m_NotDoneInit = false;
            };
        }
        public static function get ShowingIdleDialog():Boolean{
            return (m_showingIdleDialog);
        }
        public static function markDialogAsSkipped(_arg1:int):void{
            m_SkippedFlags[_arg1] = true;
        }
        private static function test_AddNeighbor():Boolean{
            var _local1 = !((Global.player.neighbors == null));
            var _local2:Boolean = ((_local1) && ((Global.player.neighbors.length <= 10)));
            var _local3 = !(test_flashSale());
            if (Global.player.neighbors.length <= 1){
                m_IdleDialogData[IDLE_ADDNEIGHBOR][m_IdleData_PROBABILITY] = 4;
            } else {
                if (Global.player.neighbors.length <= 3){
                    m_IdleDialogData[IDLE_ADDNEIGHBOR][m_IdleData_PROBABILITY] = 3;
                } else {
                    if (Global.player.neighbors.length <= 5){
                        m_IdleDialogData[IDLE_ADDNEIGHBOR][m_IdleData_PROBABILITY] = 2;
                    } else {
                        if (Global.player.neighbors.length <= 10){
                            m_IdleDialogData[IDLE_ADDNEIGHBOR][m_IdleData_PROBABILITY] = 1;
                        };
                    };
                };
            };
            return (((((_local1) && (_local2))) && (_local3)));
        }
        private static function test_HelpFriend():Boolean{
            var _local1 = (Global.player.friends.length > 3);
            var _local2 = !(test_flashSale());
            return (((_local1) && (_local2)));
        }
        private static function test_SendAGift():Boolean{
            var _local1 = (Global.player.friends.length > 1);
            var _local2 = !(test_flashSale());
            return (((_local1) && (_local2)));
        }
        private static function test_AddFarmville():Boolean{
            var _local1:Boolean;
            var _local2 = !(test_flashSale());
            if (Global.player.isNewPlayer){
                m_IdleDialogData[IDLE_ADDNEIGHBOR][m_IdleData_PROBABILITY] = 8;
            } else {
                m_IdleDialogData[IDLE_ADDNEIGHBOR][m_IdleData_PROBABILITY] = 3;
            };
            return (((_local1) && (_local2)));
        }
        private static function test_LostPuppy():Boolean{
            var _local1:Boolean;
            var _local2 = !(test_flashSale());
            return (((_local1) && (_local2)));
        }
        private static function test_flashSale():Boolean{
            return (Global.flashSaleManager.isFlashSaleAvailable);
        }
        private static function insertValidIdleChoice(_arg1:int):void{
            var _local2:int;
            while (_local2 < m_IdleDialogData[_arg1][m_IdleData_PROBABILITY]) {
                m_PossibleChoices.push(_arg1);
                _local2++;
            };
        }
        private static function initializeIdleDialogChoices():void{
            m_PossibleChoices.splice(0, m_PossibleChoices.length);
            var _local1:int;
            while (_local1 < IDLE_COUNT) {
                if (((m_IdleDialogData[_local1][m_IdleData_VALIDFUNCTION]()) && (!(m_SkippedFlags[_local1])))){
                    insertValidIdleChoice(_local1);
                };
                _local1++;
            };
        }
        private static function pickRandomIdleDialog():Boolean{
            if (m_PossibleChoices.length == 0){
                return (false);
            };
            m_PickedDialog = Math.round(Utilities.randBetween(0, (m_PossibleChoices.length - 1)));
            if (m_PickedDialog < m_PossibleChoices.length){
                m_PickedDialog = m_PossibleChoices[m_PickedDialog];
            } else {
                m_PickedDialog = 0;
            };
            return (true);
        }
        private static function GetDialogText():String{
            var _local2:String;
            var _local3:int;
            var _local4:Player;
            var _local1 = "";
            switch (m_PickedDialog){
                case IDLE_HELPFRIEND:
                    _local2 = "Sam";
                    strFriendHelpID = Player.FAKE_USER_ID_STRING;
                    if (Global.player.neighbors.length > 0){
                        _local3 = Math.round(Utilities.randBetween(0, Global.player.neighbors.length));
                        strFriendHelpID = Global.player.neighbors[_local3];
                        _local4 = Global.player.findFriendById(strFriendHelpID);
                        if (_local4){
                            _local2 = _local4.firstName;
                        };
                    };
                    _local1 = ZLoc.t("Dialogs", m_IdleDialogData[m_PickedDialog][m_IdleData_IDLEDIALOGTEXT], {friendName:_local2});
                    break;
                default:
                    _local1 = ZLoc.t("Dialogs", m_IdleDialogData[m_PickedDialog][m_IdleData_IDLEDIALOGTEXT]);
            };
            return (_local1);
        }
        public static function toggleHotKeyForIdlePopUp():void{
            if (toggleHotKey == true){
                toggleHotKey = false;
            } else {
                toggleHotKey = true;
            };
        }
        public static function generateRandomPopupTime():void{
            var _local1:Boolean;
            m_IdleDialogPopupDelayTime = Math.round(Utilities.randBetween(Global.gameSettings.getInt("idleDialogMin"), Global.gameSettings.getInt("idleDialogMax")));
            _local1 = RuntimeVariableManager.getBoolean("IS_TEST_ENVIRONMENT", false);
            if (((_local1) && (toggleHotKey))){
                m_IdleDialogPopupDelayTime = Math.round(Utilities.randBetween(5, 5));
            };
            m_IdleDialogPopupDelayTime = (m_IdleDialogPopupDelayTime * 1000);
        }
        private static function checkIfMouseMoved():Boolean{
            if ((((m_LastMouseX == Global.stage.mouseX)) && ((m_LastMouseY == Global.stage.mouseY)))){
                return (false);
            };
            m_LastMouseX = Global.stage.mouseX;
            m_LastMouseY = Global.stage.mouseY;
            return (true);
        }
        private static function resetIdleTimer():void{
            m_IdleTimerStart = GlobalEngine.getTimer();
            m_showingIdleDialog = false;
        }
        public static function doIdleDialogTimerCheck():void{
            doInitIfNecessary();
            if ((((m_IdleDialogPopupDelayTime == -1)) || ((toggleHotKey == true)))){
                generateRandomPopupTime();
            };
            if (m_showingIdleDialog){
                return;
            };
            var _local1:Boolean;
            if (Global.isVisiting()){
                _local1 = true;
            };
            if (Global.guide.isActive()){
                _local1 = true;
            };
            if (!_local1){
                if (checkIfMouseMoved()){
                    _local1 = true;
                };
            };
            if (_local1){
                resetIdleTimer();
                return;
            };
            var _local2:Boolean;
            var _local3:Number = (GlobalEngine.getTimer() - m_IdleTimerStart);
            if (_local3 > m_IdleDialogPopupDelayTime){
                _local2 = true;
            };
            if (_local2 == true){
                m_showingIdleDialog = true;
                triggerIdleDialog();
            };
        }
        public static function triggerIdleDialog():void{
            initializeIdleDialogChoices();
            if (pickRandomIdleDialog()){
                showRandomIdleDialog();
            };
        }
        protected static function onMarketCloseClick(_arg1:Event):void{
            if (m_catalogWindow){
                m_catalogWindow.removeEventListener(Event.CLOSE, onMarketCloseClick);
            };
            m_catalogWindow = null;
        }
        private static function showRandomIdleDialog():void{
            var _local5:GenericDialog;
            var _local6:Player;
            var _local7:Dialog;
            var _local8:GenericDialog;
            var _local9:GenericDialog;
            var _local10:GenericDialog;
            var _local1:String = GetDialogText();
            var _local2:String = m_IdleDialogData[m_PickedDialog][m_IdleData_IDLEDIALOGBUTTON];
            var _local3:String = m_IdleDialogData[m_PickedDialog][m_IdleData_IDLEDIALOGTITLE];
            var _local4 = "";
            switch (m_PickedDialog){
                case IDLE_ADDNEIGHBOR:
                    _local5 = new SamAdviceDialog(_local1, _local3, GenericDialogView.TYPE_OK, onIdleDialogClosed, _local3, null, true, 0, _local2);
                    UI.displayPopup(_local5, true, _local3, true);
                    return;
                case IDLE_HELPFRIEND:
                    _local6 = Global.player.findFriendById(strFriendHelpID);
                    if (_local6){
                        _local7 = new FriendHelpDialog(_local6, _local1, _local3, _local2, onIdleDialogClosed);
                        UI.displayPopup(_local7);
                    };
                    return;
                case IDLE_SENDGIFT:
                    _local2 = ZLoc.t("Dialogs", _local2);
                    _local8 = new GenericDialog(_local1, _local3, GenericDialogView.TYPE_CUSTOM_OK, onIdleDialogClosed, _local3, "assets/dialogs/mysteryGift.png", true, 0, "", null, _local2);
                    UI.displayPopup(_local8, true, _local3, true);
                    return;
                case IDLE_LOSTPUPPY:
                    _local2 = ZLoc.t("Dialogs", _local2);
                    _local9 = new GenericDialog(_local1, _local3, GenericDialogView.TYPE_CUSTOM_OK, onIdleDialogClosed, _local3, "assets/dialogs/lost_puppy_icon.png", true, 0, "", null, _local2);
                    UI.displayPopup(_local9);
                    return;
                case IDLE_FLASHSALE:
                    _local10 = Global.flashSaleManager.getIdleDialog();
                    UI.displayPopup(_local10);
                    return;
            };
            UI.displayMessage(_local1, GenericPopup.TYPE_OK, onIdleDialogClosed, _local3, true, _local4, _local3);
        }
        private static function onIdleDialogClosed(_arg1:GenericPopupEvent):void{
            m_IdleDialogPopupDelayTime = -1;
            m_showingIdleDialog = false;
            _arg1.target.removeEventListener(GenericPopupEvent.SELECTED, onIdleDialogClosed);
            if (_arg1.button != GenericPopup.YES){
                m_SkippedFlags[m_PickedDialog] = true;
                return;
            };
            switch (m_PickedDialog){
                case IDLE_ADDNEIGHBOR:
                    FrameManager.showTray("invite.php?ref=idle_popup");
                    break;
                case IDLE_HELPFRIEND:
                    if (strFriendHelpID != "0"){
                        GameTransactionManager.addTransaction(new TGetVisitMission(strFriendHelpID), true);
                    };
                    break;
                case IDLE_SENDGIFT:
                    FrameManager.navigateTo("gifts.php?ref=idle_popup");
                    break;
                case IDLE_LOSTPUPPY:
                    FrameManager.navigateTo("lost.php?type=lost_puppy");
                    break;
                case IDLE_FLASHSALE:
                    break;
            };
        }
        private static function onIdleDialogSkipped(_arg1:GenericPopupEvent):void{
            m_IdleDialogPopupDelayTime = -1;
            m_showingIdleDialog = false;
        }

    }
}//package Classes 
